package components.photoLayer
{
	import UIFree.uif_component;
	
	import components.photo.clt_rect;
	
	import flash.display.Bitmap;
	import flash.geom.Rectangle;
	
	import jp.maaash.ObjectDetection.ObjectDetector;
	import jp.maaash.ObjectDetection.ObjectDetectorEvent;
	import jp.maaash.ObjectDetection.ObjectDetectorOptions;

	public class clt_photoLayerOD extends clt_photoLayer {
		private	var _od: ObjectDetector;
		public function clt_photoLayerOD(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:String='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			createObjectDetector();
		}
		
		protected function createObjectDetector(): void {
			_od = new ObjectDetector();
			_od.cascade.base_window_h = 25;
			var op: ObjectDetectorOptions =  new ObjectDetectorOptions();
			op.min_size 	= 100;
			op.scale_factor = 1.2;
			_od.options 	= op;			
		}
		
		protected function superResetPhoto(bmp: Bitmap, faceRect: Rectangle): void {
			pe._restoreInfo = {
				x: faceRect.x,
				y: faceRect.y,
				width: faceRect.width,
				height: faceRect.height,
				roll: 0
			}
			super.resetPhoto(bmp);
		}
		
		override protected function resetPhoto(bmp: Bitmap): void {
			function doObjectDetect(e: ObjectDetectorEvent): void {
				_od.removeEventListener(ObjectDetectorEvent.DETECTION_COMPLETE, doObjectDetect);
				superResetPhoto(bmp, e.rects.length?e.rects[0]:null);
			}
			_od.addEventListener(ObjectDetectorEvent.DETECTION_COMPLETE, doObjectDetect);
			if ((pe.rect as clt_rect).getMaskSize()) _od.detect(bmp.bitmapData);
			else super.resetPhoto(bmp);
		}
	}
}